## Sid Meier's Civilization 4
## Copyright Firaxis Games 2005
## 
## added eventmanager for ViSa by the ViSa team.
## all the credits go to the makers of the G-Era, visit them at http://forums.civfanatics.com/showthread.php?t=184182
## the Team of ViSa thanks them gratefully!

from CvPythonExtensions import *
import CvUtil
import CvEventManager

import CvScreensInterface
import CvConfigParser
import CvDebugTools
import CvWBPopups
import PyHelpers
import Popup as PyPopup
import CvCameraControls

import CvTopCivs
import sys
import Terraform
import SettlerReligion
import Dome
import UrbanPlanner
import MigrantDestination
	
gc = CyGlobalContext()
localText = CyTranslator()
PyPlayer = PyHelpers.PyPlayer
PyInfo = PyHelpers.PyInfo

terraForm = Terraform.Terraform()
dome = Dome.Dome()
settlerReligion = SettlerReligion.SettlerReligion()
urbanPlanner = UrbanPlanner.UrbanPlannerHandler()
migrationDestination = MigrantDestination.MigrantDestination()


bGeneticEraEnable = False


def loadConfigurationData():
        
	global bGeneticEraEnable                  # enable or disable the G Era features?
	bGeneticEraEnable = False
	

	# loads the ViSa Component Control ini file
	config = CvConfigParser.CvConfigParser("ViSa-Component-Control.ini")

	# If we actually were able to open the "ViSa-Component-Control.ini" file then read in the values.
	# otherwise we'll keep the default values that were set at the top of this file.
	if(config != None):
		bGeneticEraEnable = config.getboolean("ViSa Component Control","Genetic Era", False)


# globals
###################################################
class CvGeneticEraEventManager:
	def __init__(self, eventManager):

		eventDefinition = ('RemoveWaterPopup', self.__eventRemoveWaterApply , self.__eventRemoveWaterBegin)
		eventManager.addCustomEventDefinition(CvUtil.EventRemoveWaterBegin, eventDefinition)


		# load ini settings
		loadConfigurationData()
		
		eventManager.addEventHandler("improvementBuilt", self.onImprovementBuilt)
		eventManager.addEventHandler("routeBuilt", self.onRouteBuilt)
		eventManager.addEventHandler("unitBuilt", self.onUnitBuilt)
		eventManager.addEventHandler("unitLost", self.onUnitLost)
		eventManager.addEventHandler("cityBuilt", self.onCityBuilt)
		eventManager.addEventHandler("unitMove", self.onUnitMove)

	def onImprovementBuilt(self, argsList):
		#self.parent.onImprovementBuilt(self, argsList);

		if ( bGeneticEraEnable ):
			terraForm.doImprovementBuilt(argsList)

	def onRouteBuilt(self, argsList):
		#self.parent.onRouteBuilt(self, argsList);
		
		if ( bGeneticEraEnable ):
			terraForm.doRouteBuilt(argsList)

	def onUnitBuilt(self, argsList):
		#self.parent.onUnitBuilt(self, argsList)
		
		if ( bGeneticEraEnable ):
			settlerReligion.doUnitBuilt(argsList)
			migrationDestination.doUnitBuilt(argsList)

######################################################################
########## COMMENT BY MRGENIE, the settlersreligion alters the
########## unitdata of the settler and corrupts the unitstats, therefore
########## i rewrote the creation of the unitstatsdata AFTER the
########## initialization of the unitdata by the settlerreligion mod
######################################################################

	def onUnitLost(self, argsList):
		#self.parent.onUnitLost(self, argsList)

		if ( bGeneticEraEnable ):
			migrationDestination.doUnitLost(argsList)
			urbanPlanner.doUnitLost(argsList)
			settlerReligion.doUnitLost(argsList)
		
		

	def onCityBuilt(self, argsList):
		#self.parent.onCityBuilt(self, argsList)
		
		if ( bGeneticEraEnable ):
			settlerReligion.doCityBuilt(argsList)
			dome.doCityBuilt(argsList)
			urbanPlanner.setCity(argsList)

	def onUnitMove(self, argsList):
		#self.parent.onUnitMove(self, argsList)
		
		if ( bGeneticEraEnable ):
			migrationDestination.doUnitMove(argsList)

	# added to handle terraform Mod
	def __eventRemoveWaterApply(self, playerID, userData, popupReturn):
		terraForm.eventRemoveWaterApply(playerID, userData, popupReturn)
		return
		
	def __eventRemoveWaterBegin(self, argsList):
		terraForm.eventRemoveWaterBegin(argsList)
		return
	# end of terraform



